package online.foxnull2.foxmall.product.controller;

import online.foxnull2.common.utils.R;
import online.foxnull2.foxmall.product.entity.AttrEntity;
import online.foxnull2.foxmall.product.entity.ProductAttrValueEntity;
import online.foxnull2.foxmall.product.service.AttrService;
import online.foxnull2.foxmall.product.service.ProductAttrValueService;
import online.foxnull2.foxmall.product.utils.PageUtils;
import online.foxnull2.foxmall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author foxnull2
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    private AttrService attrService;

    @Autowired
    public void setAttrService(AttrService attrService) {
        this.attrService = attrService;
    }

    private ProductAttrValueService productAttrValueService;

    @Autowired
    public void setProductAttrValueService(ProductAttrValueService productAttrValueService) {
        this.productAttrValueService = productAttrValueService;
    }

    @RequestMapping("/base/listforspu/{spuId}")
    public R baseListforspuBySpuId(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueService.baseListforspuBySpuId(spuId);
        return R.ok().put("productAttrValueEntityList", productAttrValueEntityList);
    }

    @RequestMapping("/{attrType}/list/{catelogId}")
    public R listBase(@RequestParam Map<String, Object> params,
                      @PathVariable String attrType,
                      @PathVariable Long catelogId) {
        PageUtils page = attrService.queryPage(params, attrType, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrEntity attr = attrService.getById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveCascade(attr);
        return R.ok();
    }

    @RequestMapping("/update/{spuId}")
    public R updateBySpuId(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> productAttrValueEntityList) {
        productAttrValueService.updateBySpuId(spuId, productAttrValueEntityList);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo) {
        attrService.updateCascade(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
